<template>
  <div style="padding:20px;">
    <el-form ref="form" label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="头衔">
        <el-select v-model="teacher.level">
          <!-- ===:值和类型都必须一致 -->
          <el-option label="高级讲师" value="1"/>
          <el-option label="首席讲师" value="2"/>
        </el-select>
      </el-form-item>
      <!-- 头像 -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadPath"
          class="avatar-uploader">
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="teacher.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="teacher.joinDate" type="date" placeholder="选择日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      uploadPath: 'http://localhost:8120/admin/oss/upload?module=avatar',
      teacher: {
        level: '1',
        sort: 0
      }
    }
  },
  created() {
    // 获取访问当前页面的请求参数：/teacher/edit?id=1447083629715943425
    // 路由映射配置：  /teacher/edit
    // console.log(this.$route.query.id)
    // 获取访问当前页面的路径参数： /teacher/edit/1447084118805344257
    // 路由映射配置： /teacher/edit/:id
    // console.log(this.$route.params.id)
    // 如果是更新操作  查询id对应的讲师数据回显
    if (this.$route.params.id) {
      teacherApi.getById(this.$route.params.id).then(r => {
        console.log(r.data.item)
        this.teacher = r.data.item
      })
    }
  },
  methods: {
    // 上传之前的检查
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 上传组件上传成功后的回调事件
    // 如果后端响应报文 状态码为200，上传组件认为上传成功
    handleAvatarSuccess(res, file) { // 参数1：响应对象 参数2：上传本地文件对象
    // 获取上传成功的图片地址设置给讲师对象
      if (res.success) { // 如果 R对象的code=20000才代表上传成功
        this.teacher.avatar = res.data.path
        console.log(this.teacher.avatar)
        // 强制渲染：如果vue不会自动渲染第三方组件更新的数据，可以手动让vue重新渲染
        this.$forceUpdate()
      } else {
        this.$message.warning('上传失败' + res.message)
      }
    },
    // 判断提交按钮事件
    saveOrUpdate() {
      if (this.teacher.id) {
        this.update()
      } else {
        this.save()
      }
    },
    // 更新
    update() {
      teacherApi.update(this.teacher).then(r => {
        this.$message.success('更新成功')
        // 刷新
        this.$router.push('/teacher/list')
      })
    },
    // 新增讲师按钮提交事件
    save() {
      teacherApi.save(this.teacher).then(r => {
        this.$message.success('新增成功')
        // 跳转到讲师列表页面
        // window.location = '/teacher/list' 会导致整个页面刷新
        this.$router.push('/teacher/list')// 代表整个路由配置的路由对象
        //        this.$route代表当前页面的路由对象
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
